package com.app.newiptv.adaper;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.app.newiptv.R;
import com.app.newiptv.activity.FilmDetailsActivity;
import com.app.newiptv.activity.SitcomActivity;
import com.app.newiptv.presenter.ExitPresenter;
import com.app.newiptv.utils.FocusedViewUtils;
import com.app.newiptv.utils.Paths;
import com.bumptech.glide.Glide;

import static com.app.newiptv.utils.Paths.BASEPATH;

/**
 * Created by wendal on 2017/9/17.
 */

public class RecommendAdaper extends RecyclerView.Adapter<RecommendAdaper.MyViewHolder> {
    public ExitPresenter presenter;
    private int count;
    private View focuseView;
    private RecyclerView parentRecycle;
    private View currentView;
    private Activity mActivity;

    public RecommendAdaper(ExitPresenter presenter, Activity activity) {
        this.presenter = presenter;
        this.mActivity = activity;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public void setFocuse(View view) {
        this.focuseView = view;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_playdetail_view, null);
        MyViewHolder viewHolder = new MyViewHolder(view);
        if (parentRecycle == null) {
            parentRecycle = (RecyclerView) parent;
            parentRecycle.addOnScrollListener(new MovieWorldScrollListener());
        }
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        presenter.onBindMovieView(holder, position);
        holder.setPosition(position);
    }

    @Override
    public int getItemCount() {
        return count;
    }

    class MyViewHolder extends RecyclerView.ViewHolder implements ExitPresenter.ItemViewHolder {
        private ImageView film_img_v;
        private TextView film_bottom_name;
        private int position;
        public MyViewHolder(View itemView) {
            super(itemView);
            film_img_v = (ImageView) itemView.findViewById(R.id.film_img);
            film_bottom_name = (TextView) itemView.findViewById(R.id.film_bottom_name);
            itemView.setOnClickListener(new SetOnClick());
            itemView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                @Override
                public void onFocusChange(View view, boolean b) {
                    if (b) {
                        currentView = film_img_v;
                        boolean scroll = FocusedViewUtils.arrowScroll(view);
                        if (!scroll) {
                            FocusedViewUtils.setFocusedView(focuseView, film_img_v, 20, 20, 25, 20);
                        }
                    } else {
                        FocusedViewUtils.setBlurView(focuseView, film_img_v);
                        focuseView.setVisibility(View.INVISIBLE);
                    }
                }
            });
        }
        public void setPosition(int position){
            this.position = position;
        }
        @Override
        public void setItemName(String name) {
            film_bottom_name.setText(name);
        }

        @Override
        public void setImageUrl(String url) {
            Glide.with(mActivity).load(BASEPATH + url).error(R.mipmap.iron).into(film_img_v);
        }
        public class SetOnClick implements View.OnClickListener{

            @Override
            public void onClick(View v) {
                Bundle bundle = presenter.getParams(position);
                Intent intent = new Intent();
                intent.putExtra("bundle", bundle);
                if (presenter.getVodType(position)) {
                    intent.setClass(mActivity, FilmDetailsActivity.class);
                    mActivity.startActivity(intent);
                } else if (presenter.getTelType(position)) {
                    intent.setClass(mActivity, SitcomActivity.class);
                    mActivity.startActivity(intent);
                }
            }
        }
    }

    class MovieWorldScrollListener extends RecyclerView.OnScrollListener {
        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                FocusedViewUtils.setFocusedView(focuseView, currentView, 15, 5, 20, 20);
            }
        }
    }
}
